package com.yzh.luckydraw.application.mq.producer;

import com.alibaba.fastjson.JSON;
import com.yzh.luckydraw.domain.activity.model.vo.ActivityPartakeRecordVO;
import com.yzh.luckydraw.domain.activity.model.vo.InvoiceVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import javax.annotation.Resource;

/**
 * 消息生产者
 */
@Component
public class KafkaProducer {
    private Logger logger = LoggerFactory.getLogger(KafkaProducer.class);
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;
    /**
     * MQ主题：中奖发货单
     *
     * 启动zk：zookeeper-server-start.bat  ../../config/zookeeper.properties
     * 启动kafka：kafka-server-start.bat ../../config/server.properties
     * 创建topic：kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic xxxxxx
     */
    public static final String TOPIC_INVOICE = "luckydraw-invoice";
    public static final String TOPIC_ACTIVITY_PARTAKE = "luckydraw-activity-partake";

    /**
     * 发送中奖物品发货单消息
     * @param invoiceVO
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendInvoice(InvoiceVO invoiceVO){
        String objJson = JSON.toJSONString(invoiceVO);
        logger.info("发送MQ消息(中奖发货单) topic：{} uId：{} msg：{}", TOPIC_INVOICE, invoiceVO.getuId(), objJson);
        return kafkaTemplate.send(TOPIC_INVOICE, objJson);
    }

    /**
     * 发送领取活动记录MQ
     * @param activityPartakeRecordVO
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendActivityPartakeRecord(ActivityPartakeRecordVO activityPartakeRecordVO) {
        String objJson = JSON.toJSONString(activityPartakeRecordVO);
        logger.info("发送MQ消息(领取活动记录) topic：{} bizId：{} msg：{}", TOPIC_ACTIVITY_PARTAKE, activityPartakeRecordVO.getuId(), objJson);
        return kafkaTemplate.send(TOPIC_ACTIVITY_PARTAKE, objJson);
    }
    
    
}
